package cn.sxw.ld.module.member.controller;


import cn.sxw.ld.common.dto.R;
import cn.sxw.ld.module.member.pojo.CostPageDto;
import cn.sxw.ld.module.member.pojo.CostRecord;
import cn.sxw.ld.module.member.pojo.DailyRecord;
import cn.sxw.ld.module.member.service.DailyRecordService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: SXW
 * @since: 2025年02月28日 19:51
 * @desc: 日常流水记录统计
 */
@Slf4j
@RestController
@RequestMapping("/daily")
public class DailyController {


    @Autowired
    private DailyRecordService dailyRecordService;

    /**
     * 获取日常记录分页
     *
     * @return
     */
    @PostMapping("/page")
    public R<Page> Page(@RequestBody CostPageDto CostPageDto) {
        try {
            Page pageInfo = dailyRecordService.getPage(CostPageDto);
            return pageInfo != null ?
                    R.success(pageInfo) :
                    R.error("系统繁忙，请稍后再试~~");
        } catch (Exception e) {
            log.error("消费记录分页错误:'\n'{}", e.getMessage());
            return R.error("系统繁忙，请稍后再试~~");
        }
    }

    /**
     * 添加日常收支记录
     *
     * @param dailyRecord
     * @return
     */
    @PostMapping("/addDailyRecord")
    public R<String> addDailyCostRecord(@RequestBody DailyRecord dailyRecord) {
        try {
            return dailyRecordService.addRecord(dailyRecord) ?
                    R.success("操作成功！") :
                    R.error("操作失败，请稍后再试~~");
        } catch (
                Exception e) {
            log.error("更新充值记录错误:'\n'{}", e.getMessage());
            return R.error("系统繁忙，请稍后再试~~");
        }
    }

    /**
     * 删除记录
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deletePet(@RequestParam List<Long> ids) {
        try {
            return dailyRecordService.removeBatchByIds(ids) ?
                    R.success("删除成功！") :
                    R.error("删除失败，请稍后再试~~");
        } catch (Exception e) {
            log.error("删除:'\n'{}", e.getMessage());
            return R.error("系统繁忙，请稍后再试~~");
        }
    }

    /**
     * 获取统计数据
     *
     * @param bsType
     * @return
     */
    @GetMapping("/getCountData")
    public R<Map<String, Integer>> getCountData(@RequestParam("bsType") Integer bsType) {
        try {
            Map<String, Integer> map = dailyRecordService.getCountData(bsType);
            return R.success(map);
        } catch (Exception e) {
            log.error("获取统计数据错误:'\n'{}", e.getMessage());
            return R.error("系统繁忙，请稍后再试~~");
        }
    }

    /**
     * 获取图表数据
     *
     * @return
     */
    @GetMapping("/getChartData")
    public R<Map<String, Object>> getCountChart(@RequestParam("bsType") Integer bsType) {
        try {
            return R.success(dailyRecordService.getCountChart(bsType));
        } catch (Exception e) {
            log.error("获取统计图表数据错误:'\n'{}", e.getMessage());
            return R.error("系统繁忙，请稍后再试~~");
        }
    }


}
